var creq;

function currefresh(stock) {
    if (stock == -1) {
        document.getElementById("refresh").innerText = "数据刷新于：" + new Date();
    }
    var url = "http://localhost:8080/data?stock=" + stock;
    if (window.XMLHttpRequest) {
        creq = new XMLHttpRequest();
    } else if (window.ActiveXObject) {
        creq = new ActiveXObject("Microsoft.XMLHTTP");
    }
    creq.open("GET", url, true);
    creq.onreadystatechange = ccallback;
    creq.send(null);
}

function ccallback() {
    if (creq.readyState == 4 && creq.status == 200) {
        imgrefresh(-1);
        btnrefresh();
        var res = creq.responseText;
        cshow(res);
    }
}

function cshow(str) {
    var data = str.split(',');
    document.getElementById("tcur").innerText = data[4];
    document.getElementById("ttoday").innerText = data[2];
    document.getElementById("tlast").innerText = data[3];
    document.getElementById("thigh").innerText = data[5];
    document.getElementById("tlow").innerText = data[6];
    document.getElementById("tquan").innerText = data[7];
    document.getElementById("tprice").innerText = data[8];
    document.getElementById("lbuy").innerText = data[14];
    document.getElementById("lsell").innerText = data[24];
    document.getElementById("tperc").innerText = data[29];
    if (data[30] === "-") {
        document.getElementById("tperc").style.color = '#00FF00';
    }
    else {
        document.getElementById("tperc").style.color = '#FF0000';
    }
    document.getElementById("lbquan1").innerText = data[9];
    document.getElementById("lbquan2").innerText = data[10];
    document.getElementById("lbquan3").innerText = data[11];
    document.getElementById("lbquan4").innerText = data[12];
    document.getElementById("lbquan5").innerText = data[13];
    document.getElementById("lbprice1").innerText = data[14];
    document.getElementById("lbprice2").innerText = data[15];
    document.getElementById("lbprice3").innerText = data[16];
    document.getElementById("lbprice4").innerText = data[17];
    document.getElementById("lbprice5").innerText = data[18];
    document.getElementById("lsquan1").innerText = data[19];
    document.getElementById("lsquan2").innerText = data[20];
    document.getElementById("lsquan3").innerText = data[21];
    document.getElementById("lsquan4").innerText = data[22];
    document.getElementById("lsquan5").innerText = data[23];
    document.getElementById("lsprice1").innerText = data[24];
    document.getElementById("lsprice2").innerText = data[25];
    document.getElementById("lsprice3").innerText = data[26];
    document.getElementById("lsprice4").innerText = data[27];
    document.getElementById("lsprice5").innerText = data[28];
}

var req;

function imgrefresh(id) {
    var url = "http://localhost:8080/img?id=" + id;
    if (window.XMLHttpRequest) {
        req = new XMLHttpRequest();
    } else if (window.ActiveXObject) {
        req = new ActiveXObject("Microsoft.XMLHTTP");
    }
    req.open("GET", url, true);
    req.onreadystatechange = callback;
    req.send(null);
}

function callback() {
    if (req.readyState == 4 && req.status == 200) {
        var res = req.responseText;
        show(res);
    }
}

function show(str) {
    var show = "<img class=\"img-self\" src=\"" + str + "\" />";
    document.getElementById("sp").innerHTML = show;
}

var breq;

function btnrefresh() {
    var url = "http://localhost:8080/btn";
    if (window.XMLHttpRequest) {
        breq = new XMLHttpRequest();
    } else if (window.ActiveXObject) {
        breq = new ActiveXObject("Microsoft.XMLHTTP");
    }
    breq.open("GET", url, true);
    breq.onreadystatechange = bcallback;
    breq.send(null);
}

function bcallback() {
    if (breq.readyState == 4 && breq.status == 200) {
        var res = breq.responseText;
        bshow(res);
    }
}

function bshow(str) {
    var bdata = str.split(",");
    for(var i = 0; i < bdata[0]; i++){
        document.getElementById("bperc" + i).innerText = bdata[9 * i + 1];
        if(bdata[9 * i + 2] === "-"){
            document.getElementById("bperc" + i).style.color = '#00FF00';
        }
        else{
            document.getElementById("bperc" + i).style.color = '#FF0000';
        }
        document.getElementById("bcode" + i).innerText = bdata[9 * i + 3];
        document.getElementById("bname" + i).innerText = bdata[9 * i + 4];
        document.getElementById("bcur" + i).innerText = "当前：" + bdata[9 * i + 5];
        document.getElementById("btoday" + i).innerText = "今开：" + bdata[9 * i + 6];
        document.getElementById("blast" + i).innerText = "昨收：" + bdata[9 * i + 7];
        document.getElementById("bhigh" + i).innerText = "最高：" + bdata[9 * i + 8];
        document.getElementById("blow" + i).innerText = "最低：" + bdata[9 * i + 9];
    }
}

setInterval("currefresh(-1)", "15000");